/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package unipiloto.edu.co.demo.servlet;

import org.osgi.framework.BundleContext;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import javax.ejb.EJB;
import org.osgi.framework.FrameworkUtil;
import unipiloto.edu.co.auth.service.api.UserAuthenticacionService;
import unipiloto.edu.co.demo.security.UserIdentity;
import unipiloto.edu.co.demo.service.tracker.ServiceLocator;

/**
 *
 * @author David
 */
@WebServlet(name = "LoginServlet", urlPatterns = "/login")
public class LoginServlet extends HttpServlet {

    @EJB
    private UserIdentity identity;

    @Override
    public void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, java.io.IOException {

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        out.println("<html>");
        out.println("<head>");
        out.println("<title>Ingresar</title>");
        out.println("<style type=\"text/css\">");
        out.println("@import url(\"css/style.css\");");
        out.println("</style>");
        out.println("</head>");
        out.println("<body bgcolor=white>");

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        try {
            ServiceLocator locator = new ServiceLocator(getBundleContext());
            try {
                if (locator.getUserAuthenticacionService(-1).login(username, password)) {
                    response.sendRedirect(response.encodeRedirectURL("../reports_webadmin"));
                    identity.createSession(username, password);
                    //out.println(username + ", autenticacion exitosa!");
                } else {
                    out.println("</br>");
                    out.println("<div align=center>");
                    out.println("<img src=\"img/error-icon.png\" />");
                    out.println("</div>");
                    out.println("</br>");
                    out.println("<h2 id=\"texto\">Nombre de usuario o contrasena incorrectos. Intentelo de nuevo</h2>");
                    //out.println("Nombre de usuario o contrasena incorrectos. Intentelo de nuevo");
                }
            } catch (ServiceLocator.ServiceUnavailableException e) {
                out.println("</br>");
                out.println("<div align=center>");
                out.println("<img src=\"img/error-icon.png\" />");
                out.println("</div>");
                out.println("</br>");
                out.println("<h2 id=\"texto\">El servicio no se encuentra disponible...</h2>");
                //out.println("El servicio no se encuentra disponible...");
            }

        } catch (Exception e) {
            e.printStackTrace(out);
        }
        out.println("</body>");
        out.println("</html>");
    }

    private BundleContext getBundleContext() {
        return FrameworkUtil.getBundle(UserAuthenticacionService.class).getBundleContext();
    }
}
